<?php

/*

	WebPhrame - A rudimentary PHP web site framework
	Copyright (c) 2008 Joshua Bodine

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Lesser General Public License as
	published by the Free Software Foundation, either version 3 of the
	License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this program.  If not, see
	<http://www.gnu.org/licenses/>.

*/

// Include class files
require_once("webphrame/db.php");
require_once("webphrame/conf.php");
require_once("webphrame/users.php");
require_once("webphrame/history.php");
require_once("webphrame/auth.php");

// Include function files
require_once("webphrame/util/theme.php");

// Main WebPhrame site interface class definition
class WebPhrame {
      	public $db, $conf, $users, $history, $auth;
	private $pageTitle;

	public function __construct() {
		// Load the configuration file
		if(! @include_once("config.php")) {
			// FIXME set up an auto-installation script
			$this->dieCleanly("Sorry, that database info is uncool.");
		}

		// Initialize the database
		$this->db = new DB($config);
		if(! $this->db->status) {
			$this->dieCleanly("Could not access the database. Please check your settings.");
		}

		// Initialize the site configuration handler
		$this->conf = new Conf($this->db);

		// Initialize the history handler
		$this->history = new History($this->db);

		// Initialize the users and groups handler
		$this->users = new Users($this->db, $this->conf, $this->history);

		// Initialize (or load, if existing) the auth handler
		if($_SESSION['auth'])
			$this->auth = new Auth($this->users, $this->history, $_SESSION['auth']);
		else
			$this->auth = new Auth($this->users, $this->history);
	}

	public function setPageTitle($title) {
		$this->pageTitle = $title;
	}

	public function head() {
		if($config['theme'] && theme_verify($config['theme']))
			require_once($config['theme']."/head.php");
		else
			require_once("themes/default/head.php");
	}
	public function foot() {
		if($config['theme'] && theme_verify($config['theme']))
			require_once($config['theme']."/foot.php");
		else
			require_once("themes/default/foot.php");
	}

	// Utility functions
	public function dieCleanly($reason) {
		$this->head();
		echo "<b>Sorry, an error occured".
				($reason ? ":</b> ".$reason : ".</b>")."<br>";
		$this->foot();

		exit;
	}
}

// Start the session
session_start();

// Instantiation - main web site should be able to interact with the framework
// through this object
$webphrame = new WebPhrame();

?>
